home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / progutil / iostream.zoo / src / editbuf.cc next >
Encoding:
C/C++ Source or Header  |  1991-09-22  |  18.5 KB  |  721 lines

  1. //    This is part of the iostream library, providing input/output for C++.
  2. //    Copyright (C) 1991 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include "ioprivat.h"
  19. #include "editbuf.h"
  20. #include <stddef.h>
  21. #pragma implementation
  22.  
  23. /* NOTE: Some of the code here is taken from GNU emacs */
  24. /* Hence this file falls under the GNU License! */
  25.  
  26. // Invariants for edit_streambuf:
  27. // An edit_streambuf is associated with a specific edit_string,
  28. // which again is a sub-string of a specific edit_buffer.
  29. // An edit_streambuf is always in either get mode or put mode, never both.
  30. // In get mode, gptr() is the current position,
  31. // and pbase(), pptr(), and epptr() are all NULL.
  32. // In put mode, pptr() is the current position,
  33. // and eback(), gptr(), and egptr() are all NULL.
  34. // Any edit_streambuf that is actively doing insertion (as opposed to
  35. // replacing) // must have its pptr() pointing to the start of the gap.
  36. // Only one edit_streambuf can be actively inserting into a specific
  37. // edit_buffer; the edit_buffer's _writer field points to that edit_streambuf.
  38. // That edit_streambuf "owns" the gap, and the actual start of the
  39. // gap is the pptr() of the edit_streambuf; the edit_buffer::_gap_start pointer
  40. // will only be updated on an edit_streambuf::overflow().
  41.  
  42. int edit_streambuf::truncate()
  43. {
  44.     str->buffer->delete_range(str->buffer->tell((buf_char*)pptr()),
  45.                   str->buffer->tell(str->end));
  46.     return 0;
  47. }
  48.  
  49. #ifdef OLD_STDIO
  50. inline void  disconnect_gap_from_file(edit_buffer* buffer, FILE* fp)
  51. {
  52.     if (buffer->gap_start_ptr != &fp->__bufp)
  53.     return;
  54.     buffer->gap_start_normal = fp->__bufp;
  55.     buffer->gap_start_ptr = &buffer->gap_start_normal;
  56. }
  57. #endif
  58.  
  59. void edit_streambuf::flush_to_buffer(edit_buffer* buffer)
  60. {
  61.     if (pptr() > buffer->_gap_start && pptr() < buffer->gap_end())
  62.     buffer->_gap_start = pptr();
  63. }
  64.  
  65. void edit_streambuf::disconnect_gap_from_file(edit_buffer* buffer)
  66. {
  67. #ifndef OLD_STDIO
  68.     if (buffer->_writer != this) return;
  69.     flush_to_buffer(buffer);
  70.     setp(pptr(),pptr());
  71.     buffer->_writer = NULL;    
  72. #else
  73.     if (buffer->gap_start_ptr != &__bufp)
  74.     return;
  75.     buffer->gap_start_normal = gptr();
  76.     buffer->gap_start_ptr = &buffer->gap_start_normal;
  77. #endif
  78. }
  79.  
  80. buf_index edit_buffer::tell(buf_char *ptr)
  81. {
  82.     if (ptr <= gap_start())
  83.     return ptr - data;
  84.     else
  85.     return ptr - gap_end() + size1();
  86. }
  87.  
  88. #if 0
  89. buf_index buf_cookie::tell()
  90. {
  91.     return str->buffer->tell(file->__bufp);
  92. }
  93. #endif
  94.  
  95. buf_index edit_buffer::tell(edit_mark*mark)
  96. {
  97.     return tell(data + mark->index_in_buffer(this));
  98. }
  99.  
  100. // adjust the position of the gap
  101.  
  102. void edit_buffer::move_gap(buf_offset pos)
  103. {
  104.   if (pos < size1())
  105.     gap_left (pos);
  106.   else if (pos > size1())
  107.     gap_right (pos);
  108. }
  109.  
  110. void edit_buffer::gap_left (int pos)
  111. {
  112.   register buf_char *to, *from;
  113.   register int i;
  114.   int new_s1;
  115.  
  116.   i = size1();
  117.   from = gap_start();
  118.   to = from + gap_size();
  119.   new_s1 = size1();
  120.  
  121.   /* Now copy the characters.  To move the gap down,
  122.      copy characters up.  */
  123.  
  124.   for (;;)
  125.     {
  126.       /* I gets number of characters left to copy.  */
  127.       i = new_s1 - pos;
  128.       if (i == 0)
  129.     break;
  130. #if 0
  131.       /* If a quit is requested, stop copying now.
  132.      Change POS to be where we have actually moved the gap to.  */
  133.       if (QUITP)
  134.     {
  135.       pos = new_s1;
  136.       break;
  137.     }
  138. #endif
  139.       /* Move at most 32000 chars before checking again for a quit.  */
  140.       if (i > 32000)
  141.     i = 32000;
  142.       new_s1 -= i;
  143.       while (--i >= 0)
  144.     *--to = *--from;
  145.     }
  146.  
  147.   /* Adjust markers, and buffer data structure, to put the gap at POS.
  148.      POS is where the loop above stopped, which may be what was specified
  149.      or may be where a quit was detected.  */
  150.   adjust_markers (pos << 1, size1() << 1, gap_size(), data);
  151. #ifndef OLD_STDIO
  152.   _gap_start = data + pos;
  153. #else
  154.   if (gap_start_ptr == &gap_start_normal)
  155.     gap_start_normal = data + pos;
  156. #endif
  157.   __gap_end_pos = to - data;
  158. /*  QUIT;*/
  159. }
  160.  
  161. void edit_buffer::gap_right (int pos)
  162. {
  163.   register buf_char *to, *from;
  164.   register int i;
  165.   int new_s1;
  166.  
  167.   i = size1();
  168.   to = gap_start();
  169.   from = i + gap_end();
  170.   new_s1 = i;
  171.  
  172.   /* Now copy the characters.  To move the gap up,
  173.      copy characters down.  */
  174.  
  175.   while (1)
  176.     {
  177.       /* I gets number of characters left to copy.  */
  178.       i = pos - new_s1;
  179.       if (i == 0)
  180.     break;
  181. #if 0
  182.       /* If a quit is requested, stop copying now.
  183.      Change POS to be where we have actually moved the gap to.  */
  184.       if (QUITP)
  185.     {
  186.       pos = new_s1;
  187.       break;
  188.     }
  189. #endif
  190.       /* Move at most 32000 chars before checking again for a quit.  */
  191.       if (i > 32000)
  192.     i = 32000;
  193.       new_s1 += i;
  194.       while (--i >= 0)
  195.     *to++ = *from++;
  196.     }
  197.  
  198.   adjust_markers ((size1() + gap_size()) << 1, (pos + gap_size()) << 1,
  199.     - gap_size(), data);
  200. #ifndef OLD_STDIO
  201.   _gap_start = data+pos;
  202. #else
  203.   if (gap_start_ptr == &gap_start_normal)
  204.     gap_start_normal = data + pos;
  205. #endif
  206.   __gap_end_pos = from - data;
  207. /*  QUIT;*/
  208. }
  209.  
  210. /* make sure that the gap in the current buffer is at least k
  211.    characters wide */
  212.  
  213. void edit_buffer::make_gap(buf_offset k)
  214. {
  215.   register buf_char *p1, *p2, *lim;
  216.   buf_char *old_data = data;
  217.   int s1 = size1();
  218.  
  219.   if (gap_size() >= k)
  220.     return;
  221.  
  222.   /* Get more than just enough */
  223.   if (buf_size > 1000) k += 2000;
  224.   else k += /*200;*/ 20; // for testing!
  225.  
  226.   p1 = (buf_char *) realloc (data, s1 + size2() + k);
  227.   if (p1 == 0)
  228.     abort(); /*memory_full ();*/
  229.  
  230.   k -= gap_size();            /* Amount of increase.  */
  231.  
  232.   /* Record new location of text */
  233.   data = p1;
  234.  
  235.   /* Transfer the new free space from the end to the gap
  236.      by shifting the second segment upward */
  237.   p2 = data + buf_size;
  238.   p1 = p2 + k;
  239.   lim = p2 - size2();
  240.   while (lim < p2)
  241.     *--p1 = *--p2;
  242.  
  243.   /* Finish updating text location data */
  244.   __gap_end_pos += k;
  245.  
  246. #ifndef OLD_STDIO
  247.   _gap_start = data + s1;
  248. #else
  249.   if (gap_start_ptr == &gap_start_normal)
  250.     gap_start_normal = data + s1;
  251. #endif
  252.  
  253.   /* adjust markers */
  254.   adjust_markers (s1 << 1, (buf_size << 1) + 1, k, old_data);
  255.   buf_size += k;
  256. }
  257.  
  258. /* Add `amount' to the position of every marker in the current buffer
  259.    whose current position is between `from' (exclusive) and `to' (inclusive).
  260.    Also, any markers past the outside of that interval, in the direction
  261.    of adjustment, are first moved back to the near end of the interval
  262.    and then adjusted by `amount'.  */
  263.  
  264. void edit_buffer::adjust_markers(register mark_pointer low,
  265.                  register mark_pointer high,
  266.                  int amount, buf_char *old_data)
  267. {
  268.   register struct edit_mark *m;
  269.   register mark_pointer mpos;
  270.   /* convert to mark_pointer */
  271.   amount <<= 1;
  272.  
  273.   if (_writer)
  274.       _writer->disconnect_gap_from_file(this);
  275.  
  276.   for (m = mark_list(); m != NULL; m = m->chain)
  277.     {
  278.       mpos = m->_pos;
  279.       if (amount > 0)
  280.     {
  281.       if (mpos > high && mpos < high + amount)
  282.         mpos = high + amount;
  283.     }
  284.       else
  285.     {
  286.       if (mpos > low + amount && mpos <= low)
  287.         mpos = low + amount;
  288.     }
  289.       if (mpos > low && mpos <= high)
  290.     mpos += amount;
  291.       m->_pos = mpos;
  292.     }
  293.  
  294.     // Now adjust files
  295.     edit_streambuf *file;
  296.  
  297.     for (file = files; file != NULL; file = file->next) {
  298.     mpos = file->current() - old_data;
  299.     if (amount > 0)
  300.     {
  301.       if (mpos > high && mpos < high + amount)
  302.         mpos = high + amount;
  303.     }
  304.     else
  305.     {
  306.       if (mpos > low + amount && mpos <= low)
  307.         mpos = low + amount;
  308.     }
  309.     if (mpos > low && mpos <= high)
  310.         mpos += amount;
  311.     char* new_pos = data + mpos;
  312.     if (file->is_reading()) {
  313.         file->setg(new_pos, new_pos, new_pos);
  314.         file->setp(NULL, NULL);
  315.     }
  316.     else {
  317.         file->setg(NULL, NULL, NULL);
  318.         file->setp(new_pos, new_pos);
  319.     }
  320.     }
  321. }
  322.  
  323. #if 0
  324. stdio_
  325.    __off == index at start of buffer (need only be valid after seek ? )
  326.    __buf ==
  327.  
  328. if read/read_delete/overwrite mode:
  329.      __endp <= min(*gap_start_ptr, edit_string->end->ptr(buffer))
  330.  
  331. if inserting:
  332.      must have *gap_start_ptr == __bufp && *gap_start_ptr+gap == __endp
  333.      file->edit_string->end->ptr(buffer) == *gap_start_ptr+end
  334. if write_mode:
  335.      if before gap
  336. #endif
  337.  
  338. int edit_streambuf::underflow()
  339. {
  340.     if (!(_mode & ios::in))
  341.     return EOF;
  342.     struct edit_buffer *buffer = str->buffer;
  343.     if (!is_reading()) { // Must switch from put to get mode.
  344.     disconnect_gap_from_file(buffer);
  345.     setg(pptr(), pptr(), pptr());
  346.     setp(NULL, NULL);
  347.     }
  348.     buf_char *str_end = str->end->ptr(buffer);
  349.   retry:
  350.     if (gptr() < egptr()) {
  351.     return *gptr();
  352.     }
  353.     if ((buf_char*)gptr() == str_end)
  354.     return EOF;
  355.     if (str_end <= buffer->gap_start()) {
  356.     setg(eback(), gptr(), str_end);
  357.     goto retry;
  358.     }
  359.     if (gptr() < buffer->gap_start()) {
  360.     setg(eback(), gptr(), buffer->gap_start());
  361.     goto retry;
  362.     }
  363.     if (gptr() == buffer->gap_start()) {
  364.     disconnect_gap_from_file(buffer);
  365. //    fp->__offset += fp->__bufp - fp->__buffer;
  366.     setg(buffer->gap_end(), buffer->gap_end(), str_end);
  367.     }
  368.     else
  369.     setg(eback(), gptr(), str_end);
  370.     goto retry;
  371. }
  372.  
  373. int edit_streambuf::overflow(int ch = EOF)
  374. {
  375.     if (_mode == ios::in)
  376.     return EOF;
  377.     struct edit_buffer *buffer = str->buffer;
  378.     flush_to_buffer(buffer);
  379.     if (ch == EOF)
  380.     return 0;
  381.     if (is_reading()) { // Must switch from get to put mode.
  382.     setp(gptr(), gptr());
  383.     setg(NULL, NULL, NULL);
  384.     }
  385.     buf_char *str_end = str->end->ptr(buffer);
  386.   retry:
  387.     if (pptr() < epptr()) {
  388.     *pptr() = ch;
  389.     pbump(1);
  390.     return (unsigned char)ch;
  391.     }
  392.     if ((buf_char*)pptr() == str_end || inserting()) {
  393.     /* insert instead */
  394.     if (buffer->_writer)
  395.         buffer->_writer->flush_to_buffer(); // Redundant?
  396.     buffer->_writer = NULL;
  397.     if  (pptr() >= buffer->gap_end())
  398.         buffer->move_gap(pptr() - buffer->gap_size());
  399.     else
  400.         buffer->move_gap(pptr());
  401.     buffer->make_gap(1);
  402.     setp(buffer->gap_start(), buffer->gap_end());
  403.     buffer->_writer = this;
  404.     *pptr() = ch;
  405.     pbump(1);
  406.     return (unsigned char)ch;
  407.     }
  408.     if (str_end <= buffer->gap_start()) {
  409.     // Entire string is left of gap.
  410.     setp(pptr(), str_end);
  411.     }
  412.     else if (pptr() < buffer->gap_start()) {
  413.     // Current pos is left of gap.
  414.     setp(pptr(), buffer->gap_start());
  415.     goto retry;
  416.     }
  417.     else if (pptr() == buffer->gap_start()) {
  418.     // Current pos is at start of gap; move to end of gap.
  419. //    disconnect_gap_from_file(buffer);
  420.     setp(buffer->gap_end(), str_end);
  421. //    __offset += __bufp - __buffer;
  422.     }
  423.     else {
  424.     // Otherwise, current pos is right of gap.
  425.     setp(pptr(), str_end);
  426.     }
  427.     goto retry;
  428. }
  429.  
  430. // Called by fseek(fp, pos, whence) if fp is bound to a edit_buffer.
  431.  
  432. streamoff edit_streambuf::seekoff(streamoff offset, seek_dir dir,
  433.                   int mode=ios::in|ios::out)
  434. {
  435.     struct edit_buffer *buffer = str->buffer;
  436.     disconnect_gap_from_file(buffer);
  437.     buf_index cur_pos = buffer->tell((buf_char*)current());;
  438.     buf_index start_pos = buffer->tell(str->start);
  439.     buf_index end_pos = buffer->tell(str->end);
  440.     switch (dir) {
  441.       case ios::beg:
  442.     offset += start_pos;
  443.     break;
  444.       case ios::cur:
  445.     offset += cur_pos;
  446.     break;
  447.       case ios::end:
  448.     offset += end_pos;
  449.     break;
  450.     }
  451.     if (offset < start_pos || offset > end_pos)
  452.     return EOF;
  453.     buf_char *new_pos = buffer->data + offset;
  454.     buf_char* gap_start = buffer->gap_start();
  455.     if (new_pos > gap_start) {
  456.     buf_char* gap_end = buffer->gap_end();
  457.     new_pos += gap_end - gap_start;
  458.     if (new_pos >= buffer->data + buffer->buf_size) abort(); // Paranoia.
  459.     }
  460.     if (is_reading())
  461.     setg(new_pos, new_pos, new_pos);
  462.     else
  463.     setp(new_pos, new_pos);
  464.     return EOF;
  465. }
  466.  
  467. #if 0
  468. int buf_seek(void *arg_cookie, fpos_t * pos, int whence)
  469. {
  470.     struct buf_cookie *cookie = arg_cookie;
  471.     FILE *file = cookie->file;
  472.     struct edit_buffer *buffer = cookie->str->buffer;
  473.     buf_char *str_start = cookie->str->start->ptr(buffer);
  474.     disconnect_gap_from_file(buffer, cookie->file);
  475.     fpos_t cur_pos, new_pos;
  476.     if (file->__bufp <= *buffer->gap_start_ptr
  477.     || str_start >= buffer->__gap_end)
  478.     cur_pos = str_start - file->__bufp;
  479.     else
  480.     cur_pos =
  481.         (*buffer->gap_start_ptr - str_start) + (file->__bufp - __gap_end);
  482.     end_pos = ...;
  483.     switch (whence) {
  484.       case SEEK_SET:
  485.     new_pos = *pos;
  486.     break;
  487.       case SEEK_CUR:
  488.     new_pos = cur_pos + *pos;
  489.     break;
  490.       case SEEK_END:
  491.     new_pos = end_pos + *pos;
  492.     break;
  493.     }
  494.     if (new_pos > end_pos) {
  495.     seek to end_pos;
  496.     insert_nulls(new_pos - end_pos);
  497.     return;
  498.     }
  499.     if (str_start + new_pos <= *gap_start_ptr &* *gap_start_ptr < end) {
  500.     __buffer = str_start;
  501.         __off = 0;
  502.     __bufp = str_start + new_pos;
  503.     file->__get_limit =
  504.         *buffer->gap_start_ptr; /* what if gap_start_ptr == &bufp ??? */
  505.     } else if () {
  506.     
  507.     }
  508.     *pos = new_pos;
  509. }
  510. #endif
  511.  
  512. /* Delete characters from `from' up to (but not incl) `to' */
  513.  
  514. void edit_buffer::delete_range (buf_index from, buf_index to)
  515. {
  516.   register int numdel;
  517.  
  518.   if ((numdel = to - from) <= 0)
  519.     return;
  520.  
  521.   /* Make sure the gap is somewhere in or next to what we are deleting */
  522.   if (from > size1())
  523.     gap_right (from);
  524.   if (to < size1())
  525.     gap_left (to);
  526.  
  527.   /* Relocate all markers pointing into the new, larger gap
  528.      to point at the end of the text before the gap.  */
  529.   adjust_markers ((to + gap_size()) << 1, (to + gap_size()) << 1,
  530.     - numdel - gap_size(), data);
  531.  
  532.    __gap_end_pos = to + gap_size();
  533.   _gap_start = data + from;
  534. }
  535.  
  536. void edit_buffer::delete_range(struct edit_mark *start, struct edit_mark *end)
  537. {
  538.     delete_range(tell(start), tell(end));
  539. }
  540.  
  541. void buf_delete_chars(struct edit_buffer *buf, struct edit_mark *mark, size_t count)
  542. {
  543.  abort();
  544. }
  545.  
  546. edit_streambuf::edit_streambuf(edit_string* bstr, int mode)
  547. {
  548.     _mode = mode;
  549.     str = bstr;
  550.     edit_buffer* buffer = bstr->buffer;
  551.     next = buffer->files;
  552.     buffer->files = this;
  553.     char* buf_ptr = bstr->start->ptr(buffer);
  554.     _inserting = 0;
  555. //    setb(buf_ptr, buf_ptr, 0);
  556.     if (mode & ios::out+ios::trunc+ios::app) {
  557.     // Start out in put mode.
  558.     setp(buf_ptr, buf_ptr);
  559.     setg(NULL, NULL, NULL);
  560.     }
  561.     else {
  562.     // Start out in get mode.
  563.     setp(NULL, NULL);
  564.     setg(buf_ptr, buf_ptr, buf_ptr);
  565.     }
  566.     if (_mode & ios::trunc)
  567.     truncate();
  568.     if (_mode & ios::ate)
  569.     seekoff(0, ios::end);
  570. }
  571.  
  572. // Called by fclose(fp) if fp is bound to a edit_buffer.
  573.  
  574. #if 0
  575. static int buf_close(void *arg)
  576. {
  577.     register struct buf_cookie *cookie = arg;
  578.     struct edit_buffer *buffer = cookie->str->buffer;
  579.     struct buf_cookie **ptr;
  580.     for (ptr = &buffer->files; *ptr != cookie; ptr = &(*ptr)->next) ;
  581.     *ptr = cookie->next;
  582.     disconnect_gap_from_file(buffer, cookie->file);
  583.     free (cookie);
  584.     return 0;
  585. }
  586. #endif
  587.  
  588. edit_streambuf::~edit_streambuf()
  589. {
  590.     if (_mode == ios::out)
  591.     truncate();
  592.     // Unlink this from list of files associated with bstr->buffer.
  593.     edit_streambuf **ptr = &str->buffer->files;
  594.     for (; *ptr != this; ptr = &(*ptr)->next) { }
  595.     *ptr = next;
  596.  
  597.     disconnect_gap_from_file(str->buffer);
  598. }
  599.  
  600. edit_buffer::edit_buffer()
  601. {
  602.     buf_size = /*200;*/ 15; /* for testing! */
  603.     data = malloc(buf_size);
  604.     files = NULL;
  605. #ifndef OLD_STDIO
  606.     _gap_start = data;
  607.     _writer = NULL;
  608. #else
  609.     gap_start_normal = data;
  610.     gap_start_ptr = &gap_start_normal;
  611. #endif
  612.     __gap_end_pos = buf_size;
  613.     start_mark.chain = &end_mark;
  614.     start_mark._pos = 0;
  615.     end_mark.chain = NULL;
  616.     end_mark._pos = 2 * buf_size + 1;
  617. }
  618.  
  619. // Allocate a new mark, which is adjusted by 'delta' bytes from 'this'.
  620. // Restrict new mark to lie within 'str'.
  621.  
  622. edit_mark *edit_mark::new_mark(struct edit_string *str, long delta)
  623. {
  624.     struct edit_mark *mark = new edit_mark();
  625.     struct edit_buffer *buf = str->buffer;
  626.     mark->chain = buf->start_mark.chain;
  627.     buf->start_mark.chain = mark;
  628.     int size1 = buf->size1() << 1;
  629.     int gap_size = buf->gap_size() << 1;
  630.     delta <<= 1;
  631.  
  632.     // check if new and old marks are opposite sides of gap
  633.     if (_pos <= size1 && _pos + delta > size1)
  634.     delta += gap_size;
  635.     else if (_pos >= size1 + gap_size && _pos + delta < size1 + gap_size)
  636.     delta -= gap_size;
  637.  
  638.     mark->_pos = _pos + delta;
  639.     if (mark->_pos < str->start->_pos & ~1)
  640.     mark->_pos = (str->start->_pos & ~ 1) + (_pos & 1);
  641.     else if (mark->_pos >= str->end->_pos)
  642.     mark->_pos = (str->end->_pos & ~ 1) + (_pos & 1);
  643.     return mark;
  644. }
  645.  
  646. // A (slow) way to find the buffer a mark belongs to.
  647.  
  648. edit_buffer * edit_mark::buffer()
  649. {
  650.     struct edit_mark *mark;
  651.     for (mark = this; mark->chain != NULL; mark = mark->chain) ;
  652.     // Assume that the last mark on the chain is the end_mark.
  653.     return (edit_buffer *)((char*)mark - offsetof(edit_buffer, end_mark));
  654. }
  655.  
  656. edit_mark::~edit_mark()
  657. {
  658.     // Must unlink mark from chain of owning buffer
  659.     struct edit_buffer *buf = buffer();
  660.     if (this == &buf->start_mark || this == &buf->end_mark) abort();
  661.     edit_mark **ptr;
  662.     for (ptr = &buf->start_mark.chain; *ptr != this; ptr = &(*ptr)->chain) ;
  663.     *ptr = this->chain;
  664. }
  665.  
  666. long edit_string::length() const
  667. {
  668.     ptrdiff_t delta = end->ptr(buffer) - start->ptr(buffer);
  669.     if (end->ptr(buffer) <= buffer->gap_start() ||
  670.     start->ptr(buffer) >= buffer->gap_end())
  671.     return delta;
  672.     return delta - buffer->gap_size();
  673. }
  674.  
  675. buf_char * edit_string::copy_bytes(size_t *lenp) const
  676. {
  677.     char *new_str;
  678.     int len1, len2 = 0;
  679.     buf_char *start1, *start2;
  680.     start1 = start->ptr(buffer);
  681.     if (end->ptr(buffer) <= buffer->gap_start()
  682.     || start->ptr(buffer) >= buffer->gap_end())
  683.     len1 = end->ptr(buffer) - start1;
  684.     else {
  685.     len1 = buffer->gap_start() - start1;
  686.     start2 = buffer->gap_end();
  687.     len2 = end->ptr(buffer) - start2;
  688.     }
  689.     new_str = (char*)malloc(len1 + len2 + 1);
  690.     memcpy(new_str, start1, len1);
  691.     if (len2 > 0) memcpy(new_str + len1, start2, len2);
  692.     new_str[len1+len2] = '\0';
  693.     *lenp = len1+len2;
  694.     return new_str;
  695. }
  696.  
  697. // Replace the buf_chars in 'this' with ones from 'src'.
  698. // Equivalent to deleting this, then inserting src, except tries
  699. // to leave marks in place: Marks whose offset from the start
  700. // of 'this' is less than 'src->length()' will still have the
  701. // same offset in 'this' when done.
  702.  
  703. void edit_string::assign(struct edit_string *src)
  704. {
  705.     edit_streambuf dst_file(this, ios::out);
  706.     if (buffer == src->buffer /*&& ???*/) { /* overly conservative */
  707.     size_t src_len;
  708.     buf_char *new_str;
  709.     new_str = src->copy_bytes(&src_len);
  710.     dst_file.sputn(new_str, src_len);
  711.     free (new_str);
  712.     } else {
  713.     edit_streambuf src_file(src, ios::in);
  714.     for ( ; ; ) {
  715.         int ch = src_file.sbumpc();
  716.         if (ch == EOF) break;
  717.         dst_file.sputc(ch);
  718.     }
  719.     }
  720. }
  721.